import requests
import json
import re
import os
import sys
from lxml import etree

cwd = os.getcwd()
sys.path.append(cwd)
print(cwd)

class ImageBiying(object):
    # url = 'https://cn.bing.com/images/search'
    url = 'https://cn.bing.com/images/async'
    # 匹配图片url的xpath，如：https://tse1-mm.cn.bing.net/th/id/OIP.73Ob4lcxFaLaxTKil_dnJQAAAA?w=190&h=143&c=7&o=5&pid=1.7
    img_url_xpath = '/html/body/div/div/ul/li/div/div/a/div/img/@src'
    save_dir = './biying' # 文件保存的路径
    count = 0

    # 初始化
    def __init__(self, word):
        self.word = word
        self.dir_path = os.path.join(self.save_dir, word)
        self.params = {
            'q': '黑色牌照',
            'first': '0',
            'count': '150', # 一次最多查询150张,默认35张
            'relp': '150',
            'scenario': 'ImageBasicHover',
            'datsrc': 'N_I',
            'layout': 'RowBased_Landscape',
            'mmasync': '1',
            'dgState': 'x*0_y*0_h*0_c*8_i*141_r*18',
            'IG': '548FA8C866884673A87F6CEDD8510AE1',
            'SFX': '5',
            'iid': 'images.5618',
        }
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36',
        }
        # 创建保存图片的文件夹
        self.folder_exist(self.dir_path)
        self.last_img_url = ''
        self.current_page = 0
    
    # 请求包含图片url的网页
    def parse(self):
        while True:
            self.params['first'] = str(self.current_page*150)
            response = requests.get(url=self.url, headers=self.headers, params=self.params)
            response.encoding = 'utf8'
            if response.status_code == 200:
                html = etree.HTML(response.text)
                list_img_url = html.xpath(self.img_url_xpath)

                for img_url in list_img_url:
                    self.download(img_url)
                # 判断上一页的最后一张图的url是否和这一页的最后一张图的url相同
                if self.last_img_url != list_img_url[-1]:
                    self.last_img_url = list_img_url[-1]
                    # 页数+1
                    self.current_page += 1
                else:
                    break
            else:
                break

    # 下载一张图片
    def download(self, img_url, img_type='jpg'):
        self.count += 1
        print('正在下载第%d张图片...'%self.count, img_url)
        response = requests.get(img_url, headers=self.headers)
        img_name = str(self.count) + '.' + img_type
        img_path = os.path.join(self.dir_path, img_name)
        with open(img_path, 'wb') as f:
            f.write(response.content)

    def folder_exist(self, dir_path):
        '''
        1. 作用:判断文件夹路径是否存在,不存在则创建
        2. 参数:dir_path:文件夹路径
        3. 返回值:None
        '''
        if not os.path.exists(dir_path):
            os.makedirs(dir_path)

if __name__ == '__main__':
    image = ImageBiying('黑色牌照')
    image.parse()